#include <keyboard.h>
#include <constants.h>
#define BREAK_CODE 0xF0
#define SHIFT_CODE 0xF1
int key_press_flag;       // Keypress
volatile int break_code_flag;
int shift_key_flag;
int user_input;           // Input location entered by the user
char keyboard_LUT[256] = {
0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   SHIFT_CODE,   0,   9,  '`',  0,
0,   0,   0,   0,   0,  'Q', '1',  0,   0,   0,  'Z', 'S', 'A', 'W', '2',  0,
0,  'C', 'X', 'D', 'E', '4', '3',  0,   0,  32,  'V', 'F', 'T', 'R', '5',  0,
0,  'N', 'B', 'H', 'G', 'Y', '6',  0,   0,   0,  'M', 'J', 'U', '7', '8',  0,
0,  ',', 'K', 'I', 'O', '0', '9',  0,   0,  '.', '/', 'L', ';', 'P', '-',  0,
0,   0,  39,   0,  '[', '=',  0,   0,   SHIFT_CODE,   0,   10, ']',  0,   92,  0,   0,
0,   0,   0,   0,   0,   0,   8,   0,   0,  '1',  0,  '4', '7',  0,   0,   0,
'0','.', '2', '5', '6', '8',  27,  0,   0,  '+', '3', '-', '*', '9',  0,   0,
0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
BREAK_CODE,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
 };

void keyboardInit(void){

//initialize keyboard (P1.4 -->CLOCK line, P1.5-->DATA line)
  P1SEL &= 0xCF;                // I/O for P1.4,1.5
  P1DIR &= 0xCF;                // input (P1.4,1.5)
  P1IES = 0x10;                 //Clock interupt on high to low transitions
  P1IE = 0x10;                  //Enable interupts on clock line
  P1IFG = 0x00;                 // Clear interrupt flag
  break_code_flag = 0;
  shift_key_flag = 0;
}

void Keyboard_interrupt(void) __interrupt[PORT1_VECTOR]
{
  char key;
  key_press_flag = 1;
  P1IE = 0x00;              // Disable interrupts
  key=get_scan();
  
  P1IFG = 0x00;             // Reset interrupt flag
  P1IE = 0x10; //0x04;              // Enable interrupts

  if (break_code_flag == 0){
    switch(key){
      case BREAK_CODE : 
        break_code_flag = 1;
        break;
  //    case keyboard_LUT[89] :
  //      shift_key_flag = 1;
  //      break;
      default :
        putChar(key);
        break;
    }

  }
  else if (key!=BREAK_CODE){
    break_code_flag = 0;
  }

        
//  if ((break_code_flag == 1) && (key != keyboard_LUT[240])){
//    break_code_flag = 0;
//    }
//  else {
//    if (key == keyboard_LUT[240]){
//      break_code_flag = 1;
//    }
//    else{
//      putChar(key);      //Prints Key on display
//    }
//  }
}

char get_scan(void)
{
  static int clk[50];
  static int arr[50];
  int i,j;
  char last;
  char key=0;
  for (i=0;i<50;i++)
  {
   clk[i]=P1IN&0x10; //0x04;
   arr[i]=P1IN&0x20; //0x02; 
  }
 last=1;
 j=0;
 for (i=0;i<50;i++)
 {
   if (last&&(clk[i]==0))
   {
     if (arr[i]&0x20)//0x02)
     {
       key |= 1<<j;
     }
     j++;
   }
   last=!(clk[i]==0);
 }
 key=keyboard_LUT[key];

 return(key);
}
